import { cloneDeep, } from 'lodash-es';

let noticeList: NoticeRecord[] = [
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "管理员",
    "noticeId": 1,
    "noticeTitle": "温馨提醒：2018-07-01 必哥新版本发布啦",
    "noticeType": "2",
    "noticeContent": "新版本内容",
    "status": "0"
  },
  {
    "createBy": "admin",
    "createTime": "2024-07-07 17:41:52",
    "updateBy": "",
    "updateTime": null,
    "remark": "管理员",
    "noticeId": 2,
    "noticeTitle": "维护通知：2018-07-01 必哥系统凌晨维护",
    "noticeType": "1",
    "noticeContent": "维护内容",
    "status": "0"
  }
];

const mocks: MockRecord[] = [
  {
    method: "get",
    url: "/api/system/notice/list",
    response(option) {
      const copyList = cloneDeep(noticeList);

      let resList = noticeList;

      if (option.query.pageNum) {
        resList = copyList.splice((option.query.pageNum - 1) * option.query.pageSize, option.query.pageSize);
      }

      const resData: responseType = {
        code: 200,
        msg: "查询成功",
        total: noticeList.length,
        ...(option.query.pageNum ? { rows: resList } : { data: resList })
      }

      return resData;
    }
  },
  {
    method: "get",
    url: "/api/system/notice/:noticeId",
    response(option) {
      const resNotice = noticeList.find(item => item.noticeId == option.query.noticeId);

      const resData: responseType = {
        code: resNotice ? 200 : 500,
        msg: resNotice ? "查询成功" : "查询失败",
        data: resNotice,
      }

      return resData;
    }
  },
  {
    method: "post",
    url: "/api/system/notice",
    response(option) {
      const newNotice = option.body;

      newNotice.noticeId = noticeList.length + 1;
      noticeList.unshift(newNotice);

      const resData: responseType = {
        code: 200,
        msg: "新增成功",
      }

      return resData;
    }
  },
  {
    method: "put",
    url: "/api/system/notice",
    response(option) {
      const newNotice = option.body;

      const index = noticeList.findIndex(item => item.noticeId == newNotice.noticeId);

      noticeList.splice(index, 1, newNotice);

      const resData: responseType = {
        code: 200,
        msg: "修改成功",
      }

      return resData;
    }
  },
  {
    method: "delete",
    url: "/api/system/notice/:noticeIds",
    response(option) {
      const noticeIds = option.query.noticeIds.split(',');

      const filterList = noticeList.filter(item => !noticeIds.includes(String(item.noticeId)));

      noticeList = filterList;

      const resData: responseType = {
        code: 200,
        msg: "删除成功",
      }
      return resData;
    }
  }
];

export default mocks;